package cn.huihuo.weibo.service;

import cn.huihuo.weibo.bean.ProdCommentBean;
import cn.huihuo.weibo.bean.dynamic.Dynamic;
import cn.huihuo.weibo.bean.dynamic.DynamicModule;
import cn.huihuo.weibo.bean.product.ProductUGCBean;
import cn.huihuo.weibo.bean.product.UGCBean;
import cn.huihuo.weibo.module.*;
import cn.huihuo.weibo.utils.TimeUtils;
import com.jfinal.plugin.activerecord.Page;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhengjunbo on 2015/9/25.
 */
public class ProductCommentService {

    public static final ProductCommentService service = new ProductCommentService();

    public ProductUGCBean findUGCByProdId(String prodId,String  ugcType,String pageStep , String count,String userId){
        ProductUGCBean productUGCBean = new ProductUGCBean();
        productUGCBean.setUgcBeans(new ArrayList<>());
        Page<ProdComment> prodCommentPage = ProdComment.prodComment.findUGCByType(Integer.valueOf(pageStep), Integer.valueOf(count), prodId, ugcType);
        takeUGCBean(productUGCBean, prodCommentPage, userId);

        return productUGCBean;
    }

    public ProductUGCBean findUGCByUGCId(String ugcId,String userId){
        ProductUGCBean productUGCBean = new ProductUGCBean();
        productUGCBean.setUgcBeans(new ArrayList<>());
        Page<ProdComment> prodCommentPage = ProdComment.prodComment.findUGCPageByUgcId(ugcId);
        takeUGCBean(productUGCBean, prodCommentPage,userId);
        return productUGCBean;
    }

    private void takeUGCBean(ProductUGCBean productUGCBean, Page<ProdComment> prodCommentPage,String userId) {
            productUGCBean.setTotalSize(String.valueOf(prodCommentPage.getTotalPage()));

        List<ProdComment> prodComments = prodCommentPage.getList();

        //配置整个

        for(ProdComment prodComment:prodComments){
            UGCBean ugcBean = new UGCBean();
            ugcBean.setUgcId(String.valueOf(prodComment.getInt("ugcId")));
            ugcBean.setUgcType(prodComment.getStr("ugcType"));

            ugcBean.setUserUrl("/user/"+prodComment.getStr("userId"));
            ugcBean.setUserId(prodComment.getStr("userId"));
            ugcBean.setUserAvatar(prodComment.getStr("avatar"));
            ugcBean.setUserName(prodComment.getStr("nickname"));

            ugcBean.setUrl(prodComment.getStr("url"));
            ugcBean.setCommentUrl("/ugc/" + prodComment.getInt("ugcId"));
            ugcBean.setProdComment(prodComment.getStr("comm"));

            ugcBean.setTime(TimeUtils.getTime(prodComment.getTimestamp("commTime").getTime()));
            ugcBean.setUrlDescription(getUrlTypeDescription(prodComment.getStr("ugcType")));
            ugcBean.setCommentNum(getCommentNum(prodComment.getInt("ugcId"))); //评论的数量

            ugcBean.setGoodChannelNum(String.valueOf(prodComment.getInt("goodChannel")));
            ugcBean.setNormalChannelNum(String.valueOf(prodComment.getInt("normalChannel")));
            ugcBean.setBadChannelNum(String.valueOf(prodComment.getInt("badChannel")));

            ugcBean.setChooseChannel(chooseChannel(prodComment.getInt("ugcId"), userId));



            productUGCBean.getUgcBeans().add(ugcBean);
        }
    }

    private String getUrlTypeDescription(String ugcType) {
        if(ugcType.equals("3")){
            return "[来源]";
        }
        if(ugcType.equals("4")){
            return "[购买链接]";
        }
        return null;
    }

    private String getCommentNum(Integer ugcId) {
        Long size = Comment.comment.findCommentSizeByUGC(String.valueOf(ugcId)).getLong("size");
        return String.valueOf(size);
    }

    private String chooseChannel(Integer ugcId, String userId) {
        UserChooseChannel userChooseChannel = UserChooseChannel.dao.getUserChooseChannel(ugcId,userId);
        if(userChooseChannel==null){
            return "0";
        }
        return String.valueOf(userChooseChannel.getInt("choose_channel"));
    }



    /**
     * 单挑微博的拼装方式
     * @param prodId
     * @return
     */
    public ProdCommentBean findByProdId(String prodId){
        ProdCommentBean prodCommentBean = new ProdCommentBean();

        String prodCommentId = prodId;

        ProdComment prodComment = ProdComment.prodComment.findAllComment(prodCommentId);



        if(prodComment==null){
            return null;
        }

        List<ProdComment> prodCommentList = ProdComment.prodComment.findProductComment(String.valueOf(prodComment.getInt("prodId")));

            String nickname = prodComment.getStr("nickname");
            String avatar = prodComment.getStr("avatar");//头像
            String userId = prodComment.getStr("userId");


        List<Comment> byProdComment = Comment.comment.findByProdComment(prodCommentId);


        prodCommentBean.setHid(prodCommentId);
        prodCommentBean.setUserId(userId);
        prodCommentBean.setHostAvatar(avatar);
        prodCommentBean.setHostUserUrl(userId);//这里需要内部链接
        prodCommentBean.setHostUserUrl(avatar);
        prodCommentBean.setHostUserName(nickname);

        prodCommentBean.setInfoImages(prodComment.getStr("outPicPath"));//博客图片
        prodCommentBean.setInfoPublishTime(getTime(prodComment.getTimestamp("prodCommentTime").getTime()));
        prodCommentBean.setInfoShareNum(String.valueOf(prodCommentList.size()));//有多少条评论
        prodCommentBean.setInfoPrice("$" + String.valueOf(prodComment.getDouble("price")));
        prodCommentBean.setInfoZanUsers(new ArrayList<>());
        prodCommentBean.setInfoTitle(prodComment.getStr("prodTitle"));
        prodCommentBean.setInfoDescrition(prodComment.getStr("prodComment"));
        prodCommentBean.setInfoShareUrl(prodId);//连接时prodId到后期再拼接上去
        prodCommentBean.setInfoCommentsNum(String.valueOf(byProdComment.size()));
        return prodCommentBean;
    }

    private String getTime(long commentTime) {
        return TimeUtils.getTime(commentTime);
    }

    public Dynamic assembleDynamic(Integer pageStep,Integer count){
        Dynamic dynamic = new Dynamic();

        dynamic.setDynamicModules(new ArrayList<>());
        Page<ProdComment> allDynamic = ProdComment.prodComment.findAllDynamic(pageStep, count);
        dynamic.setTotalSize(String.valueOf(allDynamic.getTotalPage()));


        findDynamic(dynamic, allDynamic);
        return dynamic;
    }

    public Dynamic assembleDynamic(Integer pageStep,Integer count,String userId){
        Dynamic dynamic = new Dynamic();

        dynamic.setDynamicModules(new ArrayList<>());
        Page<ProdComment> allDynamic = ProdComment.prodComment.findAllDynamic(pageStep, count,userId);
        dynamic.setTotalSize(String.valueOf(allDynamic.getTotalPage()));


        findDynamic(dynamic, allDynamic);
        return dynamic;
    }

    private void findDynamic(Dynamic dynamic, Page<ProdComment> allDynamic) {
        List<ProdComment> prodComments = allDynamic.getList();
        for(ProdComment prodComment:prodComments){
            DynamicModule dynamicModule = new DynamicModule();
            dynamicModule.setUserUrl("/user/"+prodComment.getStr("userId"));
            dynamicModule.setUserName(prodComment.get("nickname"));
            dynamicModule.setUserAvatar(prodComment.get("avatar"));
            dynamicModule.setComment(prodComment.getStr("comm"));
            dynamicModule.setProdPicUrl(useRightPic(prodComment));
            dynamicModule.setTime(TimeUtils.getTime(prodComment.getTimestamp("commTime").getTime()));
            dynamicModule.setTypeDescription(getTypeDescription(prodComment.getStr("ugcType")));
            dynamicModule.setUgcId("ugcId");

            dynamicModule.setUrl(prodComment.getStr("url"));
            dynamicModule.setUrlDescription(getUrlTypeDescription(prodComment.getStr("ugcType")));
            dynamicModule.setProdTitle(prodComment.getStr("title"));
            dynamicModule.setProdUrl("/product/"+prodComment.getInt("prodId"));
            dynamicModule.setUgcUrl("/ugc/"+prodComment.getInt("ugcId"));
            dynamic.getDynamicModules().add(dynamicModule);
        }
    }

    private String useRightPic(ProdComment prodComment) {
        if(prodComment.getStr("inner_pic_path")==null){
            return prodComment.getStr("out_pic_path");
        }

        return prodComment.getStr("inner_pic_path");
    }

    private String getTypeDescription(String ugcType) {

        if("1".equals(ugcType)){
            return "# 爬取了一个新商品";
        } else if("2".equals(ugcType)){
            return "# 添加了讨论";
        } else if("3".equals(ugcType)){
            return "# 添加了摘抄";
        } else if("4".equals(ugcType)){
            return "# 添加了购买渠道";
        }
        return "";
    }






}
